//
// Created by 吴喆 on 2020/4/16.
//

#ifndef SUMMER_GAMEUTIL_H
#define SUMMER_GAMEUTIL_H

#import <glad/glad.h>
#include <cstdlib>
#import <SOIL/stb_image_write.h>

class GameUtil {
public:
    static int screenShort(const char *fileName) {
        int viewPort[4];
        unsigned long imageSize;
        signed char *pBits = nullptr;
        int lastBuffer; // Storage for the current read buffer

//        TGAHEADER tgaHeader;

        glGetIntegerv(GL_VIEWPORT, viewPort);
        imageSize = viewPort[2] * viewPort[3] * 3;
        pBits = (GLbyte *)malloc(imageSize);
        if (pBits == nullptr) {
            return 0;
        }
        glPixelStorei(GL_PACK_ALIGNMENT, 1);
        glPixelStorei(GL_PACK_ROW_LENGTH, 0);
        glPixelStorei(GL_PACK_SKIP_ROWS, 0);
        glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
        glGetIntegerv(GL_READ_BUFFER, &lastBuffer);
        glReadBuffer(GL_FRONT);
        glReadPixels(0, 0, viewPort[2], viewPort[3], GL_RGB,
                     GL_UNSIGNED_BYTE, pBits);
        glReadBuffer(lastBuffer);
        stbi_flip_vertically_on_write(true);
        stbi_write_bmp(fileName, viewPort[2], viewPort[3], 3, pBits);
        free(pBits);
        return 1;
    }
};


#endif //SUMMER_GAMEUTIL_H
